﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartSpace.Data;
using SmartSpace.Core;

namespace SmartSpace.Workflow
{
    public interface IWorkflowService
    {
        #region metadata
        PagedList<WorkflowMetadata> FindWorkflows(int start, int limit);
        WorkflowMetadata FindWorkflow(Guid id);
        bool StoreWorkflow(WorkflowMetadata metadata);
        bool StoreDiagram(WorkflowDiagram diagram);
        WorkflowDiagram FindDiagram(Guid id);
        bool StoreEventType(EventType type);
        List<EventType> FindEventTypes();
        #endregion

        /// <summary>
        /// 获取用户可以访问的视图信息
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        ViewMetadata GetView(ActivityMetadata activity, WfViewType viewMode, WfState state, User user);

        /// <summary>
        /// 获取用户在流程驱动的时候可以选择的操作人列表
        /// </summary>
        /// <param name="event"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        List<WorkflowUser> GetEventReceivers(EventMetadata @event, User user);

        /// <summary>
        /// 获取流程驱动事件信息
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="data"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        List<EventMetadata> GetEvents(ActivityMetadata activity, DataInstance data, User user);

        /// <summary>
        /// 获取列表数据
        /// </summary>
        PagedList<DataInstance> GetDatas(WorkflowMetadata workflow, ActivityMetadata activity, WfViewType viewMode, WfState state, User user, int start, int limit);

        /// <summary>
        /// 获取详细信息
        /// </summary>
        DataInstance GetData(ViewMetadata view, User user, Guid id);

        /// <summary>
        /// 保存数据
        /// </summary>
        DataInstance Save(ViewMetadata view, DataInstance data, User user);

        ViewMetadata GetMetadata(ActivityMetadata activityMetadata);

        /// <summary>
        /// 启动流程
        /// </summary>
        /// <param name="workflow"></param>
        /// <param name="data"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        WorkflowInstance Start(WorkflowMetadata workflow, ActivityMetadata activity, DataInstance data, User user);

        /// <summary>
        /// 获取流程实例
        /// </summary>
        /// <param name="instanceId"></param>
        /// <returns></returns>
        WorkflowInstance GetInstance(Guid instanceId);
        WorkflowInstance GetInstance(Guid workflowId, Guid dataId);
        WorkflowInstance GetInstance(DataInstance data);

        /// <summary>
        /// 驱动
        /// </summary>
        bool Drive(WorkflowMetadata workflow, ActivityMetadata activity, EventMetadata @event, Guid dataId, User sender, IEnumerable<Guid> receivers);

        bool Drive(WorkflowInstance instance, EventMetadata @event, DataInstance data, User sender, User receiver);

        PagedList<WorkflowInstance> GetJobs(User user, int start, int limit);

        List<WfDriveLog> GetDriveHistories(WorkflowInstance instance);
    }
}
